package com.ywc.tools.notification;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.ywc.tools.MainActivity;
import com.ywc.tools.R;

public class NotificationActivity extends AppCompatActivity{

    private int notificationId=123;
    private NotificationManager notificationManager;
    final String CHANNEL_ID = "channel_id_1";
    final String CHANNEL_NAME = "channel_name_1";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        init();
    }

    private void init() {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        ((TextView) findViewById(R.id.bar_center)).setText("通知");
    }

    /**
     * 小图标，由 setSmallIcon()设置
     * 标题，由 setContentTitle()设置
     * 详细文本，由 setContentText()设置
     * 有效的通知渠道 ID，由 setChannelId()设置
     */

    private void initSend(boolean pending) {

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
        {
            //只在Android O之上需要渠道
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道，
            //通知才能正常弹出
            // 开启指示灯，如果设备有的话。
            notificationChannel.enableLights(true);
            // 开启震动
            notificationChannel.enableVibration(true);
            //  设置指示灯颜色
            notificationChannel.setLightColor(Color.RED);
            // 设置是否应在锁定屏幕上显示此频道的通知
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            // 设置是否显示角标
            notificationChannel.setShowBadge(true);
            //  设置绕过免打扰模式
            notificationChannel.setBypassDnd(true);
            // 设置震动频率
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400});
            //最后在notificationmanager中创建该通知渠道


            notificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("通知标题")
                .setContentText("通知内容");
        if (pending)
        {
            Intent intent = new Intent(this, PendingActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, 0);
            builder.setContentIntent(pendingIntent);
        }
        Notification build = builder.build();
        notificationManager.notify(1,build);


    }

    public void clickUi(View view) {
        switch (view.getId())
        {

            case R.id.send:
                initSend(false);
                break;
            case R.id.sendPending:
                initSend(true);
                break;
            case R.id.clear:
                notificationManager.cancel(1);
                break;
            case R.id.deleter:
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
                {
                    //删除渠道通知
                    notificationManager.deleteNotificationChannel("channel_id");
                }
                break;
        }
    }
}
